import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { SelectC, InputC, ModalC, SingleDateC } from '../../../../../components';
import moment from 'moment';
import style from '../index.less';

const FormItemLayout = {
  labelCol: {
    span: 24,
  },
  wrapperCol: {
    span: 24,
  },
};

class BussinessEdit extends PureComponent {
  static defaultProps = {
    onRef: () => {},
  };

  constructor() {
    super();
    this.state = {
      visible: true,
    };
  }

  componentDidMount() {}

  // 清空表单
  reset = () => {
    const { form } = this.props;
    form.resetFields();
  };

  // 表单内容
  formContent = () => {
    const { info, type, objectives } = this.props;
    const {
      globalM: {
        dataDictionary: { SPPI_BUSSINESS_OBJECTIVES: bussinessObjectivesList = [], SPPI_IS_COMMON = [] },
      },
    } = this.props;
    return (
      <div className={style.bussinesEdit}>
        <SelectC
          dataIndex='bussinessObjectives'
          form={this.props.form}
          title='业务目标'
          disabled={true}
          initialValue={type < 3 ? objectives : info.bussinessObjectives}
          formItemLayout={FormItemLayout}
          dataSource={bussinessObjectivesList.map((item) => {
            return { value: item.code, title: item.value };
          })}
        />
        <SelectC
          dataIndex='isRelatedInterest'
          form={this.props.form}
          title='绩效评估是否与利息收入、合同报酬相关，数据选择'
          disabled={!!(type == '3' || type == '4')}
          initialValue={info.isRelatedInterest}
          formItemLayout={FormItemLayout}
          dataSource={SPPI_IS_COMMON.map((item) => {
            return { value: item.code, title: item.value };
          })}
        />

        <SelectC
          dataIndex='isRelatedValue'
          form={this.props.form}
          title='绩效评估是否与公允价值、处分损益相关'
          disabled={!!(type == '3' || type == '4')}
          initialValue={info.isRelatedValue}
          formItemLayout={FormItemLayout}
          dataSource={SPPI_IS_COMMON.map((item) => {
            return { value: item.code, title: item.value };
          })}
        />

        <SelectC
          dataIndex='isCurrentYearHandle'
          form={this.props.form}
          title='最近一年是否有处置'
          initialValue={info.isCurrentYearHandle}
          formItemLayout={FormItemLayout}
          dataSource={SPPI_IS_COMMON.map((item) => {
            return { value: item.code, title: item.value };
          })}
          disabled={!!(type == '3' || type == '4')}
        />

        <SingleDateC
          form={this.props.form} // form对象
          title='最近一次处置时间' // 显示名称
          dataIndex='lastHandleDate' // Form识别的下拉选项Item ID
          initialValue={info.lastHandleDate ? moment(info.lastHandleDate) : undefined}
          formItemLayout={FormItemLayout}
          disabled={!!(type == '3' || type == '4')}
        />

        <InputC
          title='处分原因'
          dataIndex='reason'
          placeholder='请输入'
          initialValue={info.reason}
          form={this.props.form}
          formItemLayout={FormItemLayout}
          disabled={!!(type == '3' || type == '4')}
          showALLValue={true}
        />
      </div>
    );
  };

  handleOk = () => {
    const {
      form: { getFieldsValue },
      type,
    } = this.props;
    if (type < 3) {
      // 详情不可编辑 不进入
      const values = getFieldsValue();
      if (values.lastHandleDate) {
        values.lastHandleDate = moment(values.lastHandleDate).format('YYYY-MM-DD');
      }
      this.props.getObjectEdit(values); // 传给父组件存储
    }
    this.props.onClose(false);
  };

  handleCancel = () => {
    this.props.onClose(false);
  };

  render() {
    const { type } = this.props;
    const modalProps = {
      visible: this.state.visible,
      title: '补充编辑',
      width: 360,
      footer: type == '3' || type == '4' ? null : undefined,
    };
    return (
      <div>
        <ModalC {...modalProps} onOk={this.handleOk} onCancel={this.handleCancel}>
          {this.formContent()}
        </ModalC>
      </div>
    );
  }
}

export default connect(({ bondTradeM, globalM }) => ({
  bondTradeM,
  globalM,
}))(BussinessEdit);
